home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 8434 / 8434.xpi / chrome / content / edit.js < prev    next >
Text File  |  2008-11-16  |  12KB  |  337 lines

  1. const CSSXULNS = "@namespace url(http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul);";
  2. const CSSHTMLNS = "@namespace url(http://www.w3.org/1999/xhtml);";
  3. var style = null;
  4. var initialCode = "";
  5. var appliedCode = null;
  6. var updateOriginalCode = false;
  7. var updateURL = null;
  8. var triggeringDocument = null;
  9. var installPingURL = null;
  10. var newStyle = false;
  11. var keepStyleOnClose = false;
  12. var descriptionElement, codeElement, enabledElement, neverUpdateElement, STRINGS, URLS, prefs;
  13. try {
  14.     Components.classes["@mozilla.org/moz/jssubscript-loader;1"].getService(Components.interfaces.mozIJSSubScriptLoader).loadSubScript('chrome://itsalltext/content/API.js');
  15. } catch(e) {}
  16. function init() {
  17.     STRINGS = document.getElementById("strings");
  18.     URLS = document.getElementById("urls");
  19.     descriptionElement = document.getElementById("description");
  20.     codeElement = document.getElementById("code");
  21.     enabledElement = document.getElementById("enabled");
  22.     neverUpdateElement = document.getElementById("allow-updates");
  23.     prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefService);
  24.     prefs = prefs.getBranch("extensions.googleredesigned.");
  25.     var wrapLines = prefs.getBoolPref("wrap_lines");
  26.     refreshWordWrap(wrapLines);
  27.     document.getElementById("wrap-lines").checked = wrapLines;
  28.     var rainbowPicker = document.getElementById("rainbowpicker-detect");
  29.     if (getComputedStyle(rainbowPicker, "").MozBinding == "url(chrome://rainbowpicker/content/colorpicker.xml#colorpicker-button)") {
  30.         document.getElementById("pick-color-rainbowpicker").style.display = "-moz-box";
  31.         document.getElementById("pick-color").style.display = "none";
  32.     }
  33.     var o = window.arguments ? window.arguments[0] : {};
  34.     if (/uri=/.test(location.href)) {
  35.         o.uri = decodeURIComponent(location.href.substring(location.href.indexOf("uri=") + 4));
  36.     }
  37.     if ("uri" in o) {
  38.         style = new GoogleRedesignedStyle(o.uri);
  39.         newStyle = !style.code;
  40.         if ("description" in o) {
  41.             style.description = o.description;
  42.         }
  43.         descriptionElement.value = style.description;
  44.         document.title = style.description;
  45.         if (style.enabled) {
  46.             appliedCode = style.code;
  47.         }
  48.         if ("code" in o) {
  49.             updateOriginalCode = true;
  50.             style.code = o.code;
  51.         }
  52.         if ("updateURL" in o) {
  53.             style.updateURL = o.updateURL;
  54.         }
  55.         if ("installPingURL" in o) {
  56.             installPingURL = o.installPingURL;
  57.         }
  58.         if ("triggeringDocument" in o) {
  59.             triggeringDocument = o.triggeringDocument;
  60.         }
  61.     } else {
  62.         document.title = STRINGS.getString("addStyle");
  63.         style = new GoogleRedesignedStyle;
  64.         style._enabled = true;
  65.         if ("code" in o) {
  66.             style.code = o.code;
  67.         }
  68.         newStyle = true;
  69.     }
  70.     initialCode = style.code;
  71.     codeElement.value = style.code;
  72.     enabledElement.checked = newStyle ? true : style.enabled;
  73.     neverUpdateElement.checked = !style.neverUpdate;
  74.     if ("windowtype" in o) {
  75.         document.documentElement.setAttribute("windowtype", o.windowtype);
  76.     }
  77.     if (newStyle && style.description) {
  78.         document.getElementById("basic-view").style.display = "-moz-box";
  79.         document.getElementById("advanced-view").style.display = "-moz-box";
  80.         refreshBasicDisplay();
  81.         document.getElementById("deck").selectedIndex = prefs.getIntPref("newStyleView");
  82.     }
  83.     var internalURI = style.uri.substring(0, 3) == "rdf" ||
  84.         style.uri.substring(0, 3) == "urn";
  85.     if (internalURI) {
  86.         neverUpdateElement.style.display = "none";
  87.         if (newStyle) {
  88.             document.getElementById("userstyles-link").style.display = "inline";
  89.         } else {
  90.             var link = document.getElementById("post-to-userstyles");
  91.             link.setAttribute("href", URLS.getFormattedString("postToUserstylesUrl", [encodeURIComponent(style.uri)]));
  92.             link.style.display = "inline";
  93.         }
  94.     } else {
  95.         var link = document.getElementById("style-url-link");
  96.         link.setAttribute("href", style.uri);
  97.         link.style.display = "inline";
  98.     }
  99.     if (newStyle) {
  100.         enabledElement.style.display = "none";
  101.     }
  102. }
  103. function validate() {
  104.     if (!previewOnly && (descriptionElement.value == null || descriptionElement.value == "")) {
  105.         descriptionElement.focus();
  106.         alert(STRINGS.getString("blankDescription"));
  107.         return false;
  108.     }
  109.     if (codeElement.value == null || codeElement.value == "") {
  110.         codeElement.focus();
  111.         alert(STRINGS.getString("blankCode"));
  112.         return false;
  113.     }
  114.     return true;
  115. }
  116. function cancelDialog() {
  117.     var close = true;
  118.     if (!googleredesignedCommon.cssAreEqual(codeElement.value, initialCode)) {
  119.         var prompts = Components.classes['@mozilla.org/embedcomp/prompt-service;1'].getService(Components.interfaces.nsIPromptService);
  120.         var button = prompts.confirmEx(window, STRINGS.getString("discardChangesTitle"), STRINGS.getString("discardChangesMessage"), prompts.BUTTON_POS_0 * prompts.BUTTON_TITLE_IS_STRING + prompts.BUTTON_POS_1 * prompts.BUTTON_TITLE_IS_STRING, STRINGS.getString("discardChangesDiscard"), STRINGS.getString("discardChangesReturn"), null, null, {value: false});
  121.         close = button == 0;
  122.     }
  123.     if (close) {
  124.         if (appliedCode &&
  125.             (appliedCode != style.code || newStyle || !style.enabled)) {
  126.             googleredesignedCommon.unregisterStyle(style.uri, appliedCode);
  127.             if (!newStyle && style.enabled) {
  128.                 style.register();
  129.             }
  130.         }
  131.     }
  132.     return close;
  133. }
  134. function refreshBasicDisplay() {
  135.     loadAppliesTo();
  136.     var simpleDescription = document.getElementById("simple-description");
  137.     while (simpleDescription.firstChild) {
  138.         simpleDescription.removeChild(simpleDescription.firstChild);
  139.     }
  140.     simpleDescription.appendChild(document.createTextNode(STRINGS.getFormattedString("basicIntro", [descriptionElement.value])));
  141. }
  142. function saveViewPreference() {
  143.     prefs.setIntPref("newStyleView", document.getElementById("deck").selectedIndex);
  144. }
  145. function startLoadStylesheet() {
  146.     var consoleService = Components.classes['@mozilla.org/consoleservice;1'].getService(Components.interfaces.nsIConsoleService);
  147.     var errorListener = new CSSErrorListener;
  148.     var doc = setUpDocument();
  149.     consoleService.registerListener(errorListener);
  150.     var loadedListener = new GoogleRedesignedStylesheetLoadedListener(doc, errorListener, endStylesheetLoad);
  151.     doc.documentElement.appendChild(setUpLink(doc));
  152.     loadedListener.checkStyleLoaded();
  153. }
  154. function setUpDocument() {
  155.     return document.implementation.createDocument(googleredesignedCommon.XULNS, "googleredesigned-parse", null);
  156. }
  157. function setUpLink(doc) {
  158.     var link = doc.createElementNS(googleredesignedCommon.HTMLNS, "link");
  159.     link.rel = "stylesheet";
  160.     link.type = "text/css";
  161.     link.href = googleredesignedCommon.codePrefix + codeElement.value;
  162.     return link;
  163. }
  164. function loadAppliesTo() {
  165.     var doc = setUpDocument();
  166.     var loadedListener = new GoogleRedesignedStylesheetLoadedListener(doc, null, endStylesheetLoadAppliesTo);
  167.     doc.documentElement.appendChild(setUpLink(doc));
  168.     loadedListener.checkStyleLoaded();
  169. }
  170. function endStylesheetLoadAppliesTo(success, data) {
  171.     if (!success) {
  172.         return;
  173.     }
  174.     style.calculateMetadata(data.stylesheet);
  175.     var appliesTo = document.getElementById("applies-to");
  176.     while (appliesTo.firstChild) {
  177.         appliesTo.removeChild(appliesTo.firstChild);
  178.     }
  179.     var a = style.appliesToDisplayArray;
  180.     var frag = document.createDocumentFragment();
  181.     for (var i = 0; i < a.length; i++) {
  182.         var li = document.createElementNS(googleredesignedCommon.HTMLNS, "li");
  183.         li.appendChild(document.createTextNode(a[i]));
  184.         frag.appendChild(li);
  185.     }
  186.     appliesTo.appendChild(frag);
  187. }
  188. function cleanError(error) {
  189.     try {
  190.         return error.QueryInterface(Components.interfaces.nsIScriptError).errorMessage;
  191.     } catch (ex) {
  192.         return error.message;
  193.     }
  194. }
  195. function endStylesheetLoad(success, data) {
  196.     if (!success) {
  197.         throw data.exception;
  198.     }
  199.     if (data.errors.length > 0) {
  200.         var errorString;
  201.         for (var i = 0; i < data.errors.length; i++) {
  202.             if (errorString) {
  203.                 errorString += "\n\n" + cleanError(data.errors[i]);
  204.             } else {
  205.                 errorString = cleanError(data.errors[i]);
  206.             }
  207.         }
  208.         var prompts = Components.classes['@mozilla.org/embedcomp/prompt-service;1'].getService(Components.interfaces.nsIPromptService);
  209.         if (1 == prompts.confirmEx(window, STRINGS.getString("cssErrorDialogTitle"), STRINGS.getFormattedString("cssErrorText", [errorString]), prompts.BUTTON_POS_0 * prompts.BUTTON_TITLE_IS_STRING + prompts.BUTTON_POS_1 * prompts.BUTTON_TITLE_IS_STRING, STRINGS.getString("cssErrorAccept"), STRINGS.getString("cssErrorCancel"), null, null, {})) {
  210.             return;
  211.         }
  212.     }
  213.     if (previewOnly) {
  214.         preview();
  215.         return;
  216.     }
  217.     style.calculateMetadata(data.stylesheet);
  218.     if (appliedCode) {
  219.         googleredesignedCommon.unregisterStyle(style.uri, appliedCode);
  220.     }
  221.     if (installPingURL) {
  222.         var req = new XMLHttpRequest;
  223.         req.open("GET", installPingURL, true);
  224.         req.send(null);
  225.     }
  226.     googleredesignedCommon.dispatchEvent(triggeringDocument, "styleInstalled");
  227.     style.description = descriptionElement.value;
  228.     style.code = codeElement.value;
  229.     style.enabled = enabledElement.checked;
  230.     style.neverUpdate = !neverUpdateElement.checked;
  231.     if (updateOriginalCode) {
  232.         style.originalCode = initialCode;
  233.     }
  234.     if (updateURL) {
  235.         style.updateURL = updateURL;
  236.     }
  237.     style.customized = !googleredesignedCommon.cssAreEqual(initialCode, style.code);
  238.     style.save();
  239.     googleredesignedCommon.reloadManage();
  240.     keepStyleOnClose = true;
  241.     close();
  242. }
  243. function dialogClosing() {
  244.     if (!keepStyleOnClose && newStyle) {
  245.         GoogleRedesignedStyle.prototype.ds.deleteRecursive(style.node);
  246.         GoogleRedesignedStyle.prototype.ds.save();
  247.     }
  248. }
  249. function doPreview() {
  250.     previewOnly = true;
  251.     save();
  252. }
  253. function preview() {
  254.     if (appliedCode) {
  255.         googleredesignedCommon.unregisterStyle(style.uri, appliedCode);
  256.     }
  257.     appliedCode = codeElement.value;
  258.     googleredesignedCommon.registerStyle(style.uri, codeElement.value);
  259. }
  260. function save() {
  261.     if (!validate()) {
  262.         return false;
  263.     }
  264.     startLoadStylesheet();
  265.     return false;
  266. }
  267. function postStyle() {
  268.     previewOnly = false;
  269.     if (!validate()) {
  270.         return false;
  271.     }
  272.     startLoadStylesheet();
  273.     return true;
  274. }
  275. function ok() {
  276. previewOnly = false;
  277. return save();
  278. }
  279. function applySpecifySite(data) {
  280.     if (data.length == 0) {
  281.         return;
  282.     }
  283.     var selector = "";
  284.     for (var i = 0; i < data.length; i++) {
  285.         if (selector != "") {
  286.             selector += ", ";
  287.         }
  288.         selector += data[i].type + "(" + data[i].site + ")";
  289.     }
  290.     selector = "@-moz-document " + selector + " {\n";
  291.     if (codeElement.selectionStart != codeElement.selectionEnd) {
  292.         var selection = codeElement.value.substring(codeElement.selectionStart, codeElement.selectionEnd);
  293.         var newValue = "";
  294.         if (codeElement.selectionStart > 0) {
  295.             newValue = codeElement.value.substring(0, codeElement.selectionStart) + "\n";
  296.         }
  297.         newValue += selector;
  298.         var newCaretPosition = newValue.length;
  299.         newValue += selection + "\n}";
  300.         if (codeElement.selectionEnd < codeElement.value.length) {
  301.             newValue += "\n" + codeElement.value.substring(codeElement.selectionEnd, codeElement.value.length);
  302.         }
  303.     } else {
  304.         if (codeElement.value.length > 0) {
  305.             var newValue = codeElement.value + "\n" + selector;
  306.             var newCaretPosition = newValue.length;
  307.             newValue += "\n}";
  308.         } else {
  309.             var newValue = selector;
  310.             var newCaretPosition = newValue.length;
  311.             newValue += "\n}";
  312.         }
  313.     }
  314.     codeElement.value = newValue;
  315.     codeElement.setSelectionRange(newCaretPosition, newCaretPosition);
  316.     codeElement.focus();
  317. }
  318. function CSSErrorListener() {
  319.     this.errors = [];
  320. }
  321. CSSErrorListener.prototype = {
  322.     QueryInterface: function(aIID) {
  323.         if (aIID.equals(Components.interfaces.nsIConsoleListener) ||
  324.             aIID.equals(Components.interfaces.nsISupports))
  325.             return this;
  326.         throw Components.results.NS_NOINTERFACE;
  327.     },
  328.     observe: function(message) {
  329.         try {
  330.             if (message.QueryInterface(Components.interfaces.nsIScriptError).sourceName.indexOf(googleredesignedCommon.codePrefix) == 0) {
  331.                 this.errors.push(message);
  332.             }
  333.         } catch (ex) {
  334.         }
  335.     }
  336. }
  337.